package com.idbk.chargestation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.idbk.chargestation.R;

import java.util.Arrays;
import java.util.List;

/**
 * 仿iOS底部弹出窗
 * @author lupc
 *
 */
public class EAlertDialog extends Dialog {

	private TextView mTextCancel;
	
	private ListView mListView;
	
	private List<CharSequence> mData;
	
	private OnClickListener mListener;
	
	public EAlertDialog(Context context) {
		super(context);		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (getWindow() != null) {
			//设置dialog默认背景为透明
			getWindow().setBackgroundDrawableResource(android.R.color.transparent);
			getWindow().setGravity(Gravity.BOTTOM);//设置Dialog显示在下方
			getWindow().setWindowAnimations(R.style.share_dialog);//设置显示动画
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.dialog_from_bottom_ios, null);//dialog布局显示
			setContentView(view);
			//设置Dialog横向全屏,纵向匹配内容
			getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			setupView(view);
		}
	}
	
	private void setupView(View view){
		mTextCancel = (TextView)view.findViewById(R.id.textview_cancel);
		mTextCancel.setText(R.string.common_cancel);
		mTextCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		
		mListView = (ListView)view.findViewById(R.id.listview_bottom_ios);
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				getContext(), 
				R.layout.item_ios_dialog,
				R.id.textview_ios_item,
				mData);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dismiss();
				if(mListener != null){
					mListener.onClick(EAlertDialog.this, position);
				}				
			}
		});
	}
	
	public EAlertDialog setItems(CharSequence[] items, final OnClickListener listener){
		mData = Arrays.asList(items);
		mListener = listener;
		return this;
	}

	public EAlertDialog setItems(List<CharSequence> items, final OnClickListener listener){
		mData = items;
		mListener = listener;
		return this;
	}
	
}
